<?php
/**
 * Creater: lqr
 * Date Time : 2018/9/29
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\Test;
use App\Http\Models\User;
use App\Http\Models\Goods;
use App\Http\Models\GoodsCoupon;
use App\Http\Models\GoodsShop;
use App\Http\Models\GoodsSales;
use App\Http\Models\GoodsCategory;
use App\Http\Models\TaobaoCategory;
use App\Http\Models\UserAppKey;
use App\Http\Models\UserAuth;
use Illuminate\Support\Facades\Cache;
use DB;
use Carbon\Carbon;
/*
* 用户开放API,4BEA0D80C978B281C44939EDA95D8C27
*/
class OpenAPIController extends Controller
{
	//检测appkey
	public function checkAppKey($phone,$key)
	{
		$user = UserAppKey::where(['phone'=>$phone,'appkey'=>$key])->first();
		if(empty($user)){
			return false;
		}
		return true;
	}
	
    //获取暴款数据
    public function bursting(Request $request)
    {
        $phone = request('phone');//获取用户ID
        $key = request('appkey');//获取用户key
        if (empty($phone) || empty($key)) {
        	return json_encode(['code' => 1, 'msg' => '参数不全，需要phone和appkey！']);
        }
		if(!$this->checkAppKey($phone,$key)){
			return json_encode(['code' => 1, 'msg' => '无效的phone和appkey！']);
		}
        $page = request('page') ? request('page') : 1;
        $key = $key . '_' . $page;
        //每页显示数据个数
        $limit = env('LIMIT_OPENAPI');
       
        $list = DB::table('goods')
                ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                ->leftjoin('users', 'goods.userid', '=', 'users.id')
                ->select('users.name as username',
                    'goods.id','goods.goodsid', 'goods.goodslink', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.goodstype',
                    'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                    'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                    'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
                ->where(array('goods.goodsstate'=>2,'goods.goodstype'=>1,'goods.couponstate' => 0))->whereRaw('goods.begintime<now()')->orderBy('goods.id', 'desc')->paginate($limit);
		return response()->json(['code' => 0, 'msg' => 'ok', 'data' => $list]);
        
    }

    //秒杀平台
    public function seckill(Request $request)
    {
        $phone = request('phone');//获取用户ID
        $key = request('appkey');//获取用户key
        if (empty($phone) || empty($key)) {
        	return json_encode(['code' => 1, 'msg' => '参数不全，需要phone和appkey！']);
        }
		if(!$this->checkAppKey($phone,$key)){
			return json_encode(['code' => 1, 'msg' => '无效的phone和appkey！']);
		}
        $whs = array(
            'goods.goodstype' => 2,
            'goods.goodsstate' => 2,
            'goods.couponstate' => 0
        );
        $rawstr = 'date_add(str_to_date(CONCAT(goods_kill.startdate," ",goods_kill.startfield,":00"),"%Y-%m-%d %H:%i:%s"), interval 1 day)>now()';
        //如果没有选择类目
        $list = DB::table('goods')
            ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
            ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
            ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
            ->leftjoin('goods_kill', 'goods.id', '=', 'goods_kill.gid')
            ->leftjoin('users', 'goods.userid', '=', 'users.id')
            ->select('users.name as username',
                'goods.id','goods.goodsid', 'goods.goodstitle', 'goods.goodslink', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.goodstype',
                'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                'goods_kill.keyword', 'goods_kill.startdate', 'goods_kill.startfield', 'goods_kill.comment',
                'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
            ->where($whs)->whereRaw($rawstr)->orderBy('startdate', 'asc')->orderBy('startfield', 'asc')->get();
        return response()->json(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    //预约商品
    public function mark(Request $request)
    {
        $phone = request('phone');//获取用户ID
        $key = request('appkey');//获取用户key
        if (empty($phone) || empty($key)) {
        	return json_encode(['code' => 1, 'msg' => '参数不全，需要phone和appkey！']);
        }
		if(!$this->checkAppKey($phone,$key)){
			return json_encode(['code' => 1, 'msg' => '无效的phone和appkey！']);
		}
        $whs = array(
            'goods.goodsstate' => 2,
            'goods.couponstate' => 0
        );
        $rawstr = 'goods.begintime>now()';        
        $page = request('page') ? request('page') : 1;
        
        //每页显示数据个数
        $limit = env('LIMIT_OPENAPI');
        $list = DB::table('goods')
                ->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
                ->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
                ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                ->leftjoin('users', 'goods.userid', '=', 'users.id')
                ->select('users.name as username',
                    'goods.id','goods.goodsid', 'goods.goodslink', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.begintime', 'goods.goodstype',
                    'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
                    'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
                    'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
                ->where($whs)->whereRaw($rawstr)->orderBy('goods.id', 'asc')->paginate($limit);
        return response()->json(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    //实时上新
    public function newly(Request $request)
    {
        $phone = request('phone');//获取用户ID
        $key = request('appkey');//获取用户key
        if (empty($phone) || empty($key)) {
        	return json_encode(['code' => 1, 'msg' => '参数不全，需要phone和appkey！']);
        }
		if(!$this->checkAppKey($phone,$key)){
			return json_encode(['code' => 1, 'msg' => '无效的phone和appkey！']);
		}
        $page = request('page') ? request('page') : 1;        
        //每页显示数据个数
        $limit = env('LIMIT_OPENAPI');
		$list = DB::table('goods')
				->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
				->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
				->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
				->leftjoin('users', 'goods.userid', '=', 'users.id')
				->select('users.name as username',
					'goods.id','goods.goodsid', 'goods.goodslink', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.goodstype',
					'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
					'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
					'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype','goods_coupon.couponstarttime','goods_coupon.couponendtime')
				->where(array('goods.goodsstate'=>2,'goods.goodstype'=>0,'goods.couponstate' => 0))->whereRaw('goods.begintime<now()')->orderBy('goods.id', 'desc')->paginate($limit);
		return response()->json(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }

    //销量排行
    public function ranking(Request $request)
    {
        $phone = request('phone');//获取用户ID
        $key = request('appkey');//获取用户key
        if (empty($phone) || empty($key)) {
        	return json_encode(['code' => 1, 'msg' => '参数不全，需要phone和appkey！']);
        }
        if(!$this->checkAppKey($phone,$key)){
        	return json_encode(['code' => 1, 'msg' => '无效的phone和appkey！']);
        }
		$type=$this->request->type ? $this->request->type : 1;//获取排行类型,默认是1,近一小时
		//默认是综合
        $ob = array(
            'name' => 'goods_sales.onehoursales',
            'val' => 'desc'
        );
        //近两小时
        if ($type == 2) {
            $ob['name'] = 'goods_sales.twohoursales';
        } elseif ($type == 3) {//全天榜
            $ob['name'] = 'goods_sales.todaysales';
        } elseif ($type == 4) {//佣金
            $ob['name'] = 'goods.commissionrate';
        }
        $list = DB::table('goods')
        		->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
        		->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
        		->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
        		->leftjoin('users', 'goods.userid', '=', 'users.id')
        		->select('users.name as username',
        			'goods.id','goods.goodsid', 'goods.goodslink', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.goodstype',
        			'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
        			'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
        			'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
        		->where(array('goods.goodsstate'=>2,'goods.goodstype'=>0,'goods.couponstate' => 0))->whereRaw('goods.begintime<now()')->orderBy($ob['name'], $ob['val'])->take(100)->get();
        return response()->json(['code' => 0, 'msg' => 'ok', 'data' => $list]);
    }
		
		//获取商品详情
		public function detail(Request $request)
		{
				$phone = request('phone');//获取用户ID
				$key = request('appkey');//获取用户key
				$gid=request('gid');
				if (empty($phone) || empty($key)|| empty($gid)) {
					return json_encode(['code' => 1, 'msg' => '参数不全，需要phone、appkey和gid！']);
				}
				if(!$this->checkAppKey($phone,$key)){
					return json_encode(['code' => 1, 'msg' => '无效的phone和appkey！']);
				}
				$list = DB::table('goods')
						->leftjoin('goods_category', 'goods.cateid', '=', 'goods_category.id')
						->leftjoin('goods_shop', 'goods.id', '=', 'goods_shop.gid')
						->leftjoin('goods_sales', 'goods.id', '=', 'goods_sales.gid')
						->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
						->leftjoin('users', 'goods.userid', '=', 'users.id')
						->select('users.name as username',
							'goods.id','goods.goodsid', 'goods.goodslink', 'goods.goodstitle', 'goods.coverimage', 'goods.commissiontype', 'goods.commissionrate', 'goods.copywriting', 'goods.copywritingimage', 'goods.origprice', 'goods.voucherprice', 'goods.created_at', 'goods.goodstype',
							'goods_coupon.couponlink', 'goods_coupon.coupontotal', 'goods_coupon.couponremain', 'goods_coupon.couponmoney', 'goods_coupon.reduceprice',
							'goods_sales.monthsales', 'goods_sales.increment', 'goods_sales.onehoursales', 'goods_sales.twohoursales', 'goods_sales.todaysales',
							'goods_category.id as type_id', 'goods_category.name as type_name',
							'goods_shop.shoptype', 'goods_shop.shopname', 'goods_shop.activitytype')
						->where(array('goods.goodsstate'=>2,'goods.goodsid'=>$gid,'goods.couponstate' => 0))->whereRaw('goods.begintime<now()')->first();
				if(empty($list)){
					return json_encode(['code' => 1, 'msg' => '不存在的商品！']);
				}
				return response()->json(['code' => 0, 'msg' => 'ok', 'data' => $list]);
		}


}